package com.flycent.fanbao.ui;

import java.util.List;

import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.FinalBitmap;
import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.annotation.view.ViewInject;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import org.json.JSONObject;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.flycent.fanbao.App;
import com.flycent.fanbao.Constants;
import com.flycent.fanbao.R;
import com.flycent.fanbao.adapter.AbsViewHolderAdapter;
import com.flycent.fanbao.bean.Joke;
import com.flycent.fanbao.ui.view.RefreshViewLoadMoreProxy;
import com.flycent.fanbao.ui.view.RefreshViewLoadMoreProxy.OnLoadDataActionListener;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

/**
 * @author fbl
 *  笑话
 */
public class JokeActivity extends BackableTitleBarActivity implements OnLoadDataActionListener{
	@ViewInject(id = R.id.joke_radio0 ,click = "onClick") RadioButton mRadio0;
	@ViewInject(id = R.id.joke_radio1 ,click = "onClick") RadioButton mRadio1;

	private PullToRefreshListView mRefreshListView;
	private RefreshViewLoadMoreProxy mLoadMoreProxy;

	private JokeAdapter mAdapter;
	private List<Joke> mJokes;
	private int pageNum = 1;
	private static String iPString  = Constants.jokeIP;
	static FinalBitmap fb;
	
	@Override
	protected void onCreate(Bundle savedInstance) {
		super.onCreate(savedInstance);
		setContentView(R.layout.activity_joke);
		FinalActivity.initInjectedView(this);
		getTitleBar().setTitle("笑话");
		fb = FinalBitmap.create(this);

		mRefreshListView = (PullToRefreshListView) findViewById(R.id.joke_list);

		mRefreshListView.getRefreshableView().setDividerHeight(getResources().getDimensionPixelOffset(R.dimen.dimen_10dp));
		mRefreshListView.getRefreshableView().setSelector(new ColorDrawable(Color.TRANSPARENT));
		mAdapter = new JokeAdapter(this, R.layout.item_joke_detail);
		mRefreshListView.setAdapter(mAdapter);
		mLoadMoreProxy = new RefreshViewLoadMoreProxy(mRefreshListView, Mode.BOTH,0,0);
		mLoadMoreProxy.setOnLoadDataActionListener(this);
		mLoadMoreProxy.refreshing();

	}

	public static class JokeAdapter extends AbsViewHolderAdapter<Joke>{

		public JokeAdapter(Context context, int layoutRes) {
			super(context, layoutRes);
		}

		@Override
		protected void bindData(int pos, Joke itemData) {
			TextView text1 = getViewFromHolder(R.id.item_time_text);
			TextView text2 = getViewFromHolder(R.id.item_content_text);

			text1.setText(itemData.getUpdatetime());
			text2.setText(itemData.getContent());

			ImageView mgif = getViewFromHolder(R.id.gif);
			if(iPString.equals(Constants.jokeIP)){
				mgif.setVisibility(View.GONE);
			}else {
				fb.display(mgif, itemData.getUrl());
//				ImageLoader.getInstance().displayImage(itemData.getUrl(), mgif);
			}

		}

	}

	private void updateJokeList(String IP,final int pageNum,final boolean isRefresh){
		mLoadMoreProxy.loadDataActionComplete(false);
		FinalHttp fh =  new FinalHttp();
		AjaxParams params = new AjaxParams();
		params.put("key", "77cc83b279c2396f5edd66bdca85b803");
		params.put("dtype", "json");
		params.put("page", pageNum+"");
		params.put("pagesize", "20");
		fh.get(IP,params,new AjaxCallBack<Object>() {

			@Override
			public void onFailure(Throwable t, int errorNo, String strMsg) {
				super.onFailure(t, errorNo, strMsg);
				App.showLog("strMsg==="+strMsg.toString());
			}

			@Override
			public void onSuccess(Object object) {
				super.onSuccess(object);
				App.showLog("object==="+object.toString());

				try {
					JSONObject jsonObject = new JSONObject(object.toString());
					String reason = jsonObject.getString("reason").toString();
					if("Success".equals(reason) ){
						Gson gson = new Gson(); 					
						mJokes = gson.fromJson(jsonObject.getJSONObject("result").getJSONArray("data").toString(),
								new TypeToken<List<Joke>>(){}.getType());

						if( isRefresh ){
							mAdapter.update(mJokes);
						}else {
							mAdapter.append(mJokes);
						}
					}else {
						App.showLog("加载失败");
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

		});

		mLoadMoreProxy.loadDataActionComplete(true);
	}


	public void onClick(View v){
		if( v == mRadio0 ){
			iPString = Constants.jokeIP;
			onRefresh(1);
			mRefreshListView.requestFocus();
			mRefreshListView.setSelection(0);
		}
		if( v == mRadio1 ){
			iPString = Constants.jokeImgIP;
			onRefresh(1);
			mRefreshListView.requestFocus();
			mRefreshListView.setSelection(0);
		}
	}


	@Override
	public void onRefresh(int page) {
		updateJokeList(iPString,1, true);
	}

	@Override
	public void onLoadMore(int page) {
		++ pageNum;
		App.showLog("pageNum=="+pageNum);
		updateJokeList(iPString,pageNum, false);

	}


}
